package com.yzd.learnjava.containers;

import java.util.BitSet;
import java.util.Random;

import com.yzd.learnjava.util.Print;

public class Bits {
	public static void printBitSet(BitSet b) {
		Print.print("bits : " + b);
		StringBuilder builder = new StringBuilder();
		for(int i = 0 ; i < b.size() ; i++) {
			builder.append(b.get(i)? "1" : "0");
		}
		Print.print("bit pattern : " + builder);
	}
	
	public static void main(String[] args) {
		Random random = new Random(47);
		byte bt = (byte)random.nextInt();
		BitSet bb = new BitSet();
		for(int i = 7 ; i >= 0; i--) {
			if(((1 << i) & bt) != 0 ) {
				bb.set(i);
			} else {
				bb.clear(i);
			}
		}
		
		Print.print("byte value : " + bt);
		printBitSet(bb);
		
		short st = (short)random.nextInt();
		BitSet bs = new BitSet();
		for(int i = 15; i >= 0; i--) {
			if(((1 << i) & st) != 0) {
				bs.set(i);
			} else {
				bs.clear(i);
			}
		}
		Print.print("short value : " + st);
		printBitSet(bs);
		
		int it = random.nextInt();
		BitSet bi = new BitSet();
		for(int i = 31; i >= 0; i--) {
			if(((1 << i) & it) != 0) {
				bi.set(i);
			} else {
				bi.clear();
			}
		}
		Print.print("int value : " + it);
		printBitSet(bi);
		
		
		BitSet b127 = new BitSet();
		b127.set(127);
		Print.print("set bit 127 : " + b127);
		
		BitSet b255 = new BitSet(65);
		b255.set(255);
		Print.print("set bit 255 : " + b255);
		
		BitSet b1023 = new BitSet(512);
		b1023.set(1023);
		b1023.set(1024);
		Print.print("bit set 1023 : " + b1023);
	}
}




















